package io.chenglicun.modules.clc.controller.dataset;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.common.utils.ExcelUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.common.validator.ValidatorUtils;
import io.chenglicun.common.validator.group.AddGroup;
import io.chenglicun.common.validator.group.DefaultGroup;
import io.chenglicun.common.validator.group.UpdateGroup;
import io.chenglicun.modules.clc.dao.dataset.ClcDataSetColumnDao;
import io.chenglicun.modules.clc.dto.dataset.ClcDataColumnDTO;
import io.chenglicun.modules.clc.dto.dataset.ClcDataSetColumnDTO;
import io.chenglicun.modules.clc.entity.dataset.ClcDataSetColumnEntity;
import io.chenglicun.modules.clc.excel.dataset.ClcDataSetColumnExcel;
import io.chenglicun.modules.clc.service.dataset.ClcDataSetColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bouncycastle.jcajce.provider.symmetric.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据集字段配置
 *
 * @author HFHQ 1986866856@qq.com
 * @since 1.0 2024-02-26
 */
@RestController
@RequestMapping("clc/clcdatasetcolumn")
@Api(tags = "数据集字段配置")
public class ClcDataSetColumnController {
    @Autowired
    private ClcDataSetColumnService clcDataSetColumnService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    public Result<PageData<ClcDataSetColumnDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<ClcDataSetColumnDTO> page = clcDataSetColumnService.page(params);

        return new Result<PageData<ClcDataSetColumnDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<ClcDataSetColumnDTO> get(@PathVariable("id") Long id) {
        ClcDataSetColumnDTO data = clcDataSetColumnService.get(id);

        return new Result<ClcDataSetColumnDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody ClcDataSetColumnDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        clcDataSetColumnService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody ClcDataSetColumnDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        clcDataSetColumnService.update(dto);

        return new Result();
    }

    @PostMapping("savebatch")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result saveBatch(@RequestBody List<ClcDataSetColumnDTO> dtos) {
        if (dtos == null || dtos.size() <= 0) {
            return new Result().ok(null);
        }
        Long setId = dtos.get(0).getDataSetId();
        Map<String, Object> params = new HashMap<>();
        params.put("setid", setId);
        List<ClcDataSetColumnDTO> list = clcDataSetColumnService.list(params);
        List<Long> ids = new ArrayList<>();
        for (ClcDataSetColumnDTO clcDataSetColumnDTO : list) {
            ids.add(clcDataSetColumnDTO.getId());
        }
        if (ids.size() > 0)
            clcDataSetColumnService.deleteBatchIds(ids);
        List<ClcDataSetColumnEntity> clcDataSetColumnEntities = ConvertUtils.sourceToTarget(dtos, ClcDataSetColumnEntity.class);
        boolean b = clcDataSetColumnService.insertBatch(clcDataSetColumnEntities);
        if (b) {
            return new Result().ok(null);
        } else {
            return new Result().error(null);
        }
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        clcDataSetColumnService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ClcDataSetColumnDTO> list = clcDataSetColumnService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "数据集字段配置", list, ClcDataSetColumnExcel.class);
    }

    @GetMapping("listbyset")
    public Result<List<ClcDataSetColumnDTO>> listbyset(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<ClcDataSetColumnDTO> list = clcDataSetColumnService.list(params);
        return new Result<List<ClcDataSetColumnDTO>>().ok(list);
    }

    @GetMapping("listall")
    @ApiOperation("所有列表")
    @LogOperation("所有列表")
    public Result<List<ClcDataSetColumnDTO>> listall() {
        List<ClcDataSetColumnDTO> list = clcDataSetColumnService.list(new HashMap<>());
        return new Result<List<ClcDataSetColumnDTO>>().ok(list);
    }

    @Autowired
    ClcDataSetColumnDao dataSetColumnDao;

    @PostMapping("sort")
    @ApiOperation("修改字段排序")
    @LogOperation("修改字段排序")
    public Result sortColumn(@RequestBody List<ClcDataSetColumnDTO> dtos) {
        List<ClcDataSetColumnEntity> entities = ConvertUtils.sourceToTarget(dtos, ClcDataSetColumnEntity.class);
        for (ClcDataSetColumnEntity entity : entities) {
            clcDataSetColumnService.updateById(entity);
        }

        return new Result().ok("");
    }

}